#include "TPM_HAL_QG.h"

void vfnTPM_SelectClock (uint8 u8clock)
{
    TPMSC_CLKSx = u8clock;
}


void vfnTPM_SetPrescalerDivider (uint8 u8prescaler) 
{
    if (u8prescaler < 8)
    {
        TPMSC_PS = u8prescaler;
    }
    else 
    {
        TPMSC_PS = TPM_PRESCALER_1;
    }
}

void vfnTPM_ChannelInterruptEnable (uint8 u8channel) 
{
    if (u8channel == 0) 
    {
        TPMC0SC_CH0IE = 1;
    }
    else if (u8channel == 1)
    {
        TPMC1SC_CH1IE = 1;
    }
    else
    {
        return;
    }
}

void vfnTPM_ClearChannelFlag (uint8 u8channel)
{
    if (u8channel == 0) 
    {
        TPMC0SC_CH0F = 0;
    }
    else if (u8channel == 1) 
    {
        TPMC1SC_CH1F = 0; 
    }
    else
    {
        return;
    }
}

void vfnTPM_ChannelInputCapture (uint8 u8channel, uint8 u8config)
{
    if(u8channel == 0)
    {
        TPMSC_CPWMS = 0;
        TPMC0SC_MS0x = 0;
        TPMC0SC_ELS0x = u8config;
    }
    else if(u8channel == 1)
    {
        TPMSC_CPWMS = 0;
        TPMC1SC_MS1x = 0;
        TPMC1SC_ELS1x = u8config;     
    }
    else
    {
        return;
    }
}

void vfnTPM_SetModulo (uint16 u16modulo)
{
    TPMMOD = u16modulo;    
}

uint16 u16TPM_CheckChannelValue(uint8 u8channel)
{
    if (u8channel == 0) 
    {
        return TPMC0V;   
    }
    else if(u8channel == 1) 
    {
        return TPMC1V;   
    }
    else
    {
        return 0;
    }
}

void vfnTPM_SetChannel1Value (uint16 u16value)
{
	TPMC1V = u16value;			
}

void vfnTPM_Channel1OutputCompare (void)
{
	TPMC1SC_MS1x = 0x01;
	TPMC1SC_ELS1x = 0x00;
}

